<template>
  <div class="app-container">
    <el-tree
      :data="depts"
      :props="defaultProps"
      :expand-on-click-node="false"
      default-expand-all
    >
      <!-- 节点结构 -->
      <!-- v-slot="{ node, data }" 只能作用在template -->
      <template v-slot="{ data }">
        <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
          <el-col>{{ data.name }}</el-col>
          <el-col :span="4">
            <span class="tree-manager">{{ data.managerName }}</span>
            <!-- $event 实参 表示类型 当前事件对象-->
            <el-dropdown @command="hCommand($event,data.id)">
              <!-- 显示区域内容 -->
              <span class="el-dropdown-link">
                操作<i class="el-icon-arrow-down el-icon--right" />
              </span>
              <!-- 下拉菜单选项 -->
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                <el-dropdown-item command="del">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </el-row>
      </template>
    </el-tree>

    <!-- 弹窗组件,添加;修改 -->
    <!-- sync 双向绑定    事件处理:子传父时: this.$emit('update:处理的事件',传入处理的值) -->
    <addDept
      ref="dept"
      :show-dialog.sync="showDialog"
      :cur-node-id.sync="curNodeId"
      :is-eait="isEait"
      @updatadep="getDepartment"
    />
  </div>
</template>

<script>
import { delDepartment, getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import addDept from './components/addDept.vue'
export default {
  components: { addDept },
  data() {
    return {
      // 添加部门用到的pid
      curNodeId: '',
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 控制显示隐藏   自定义
      showDialog: false,
      isEait: false
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async deldd(id) {
      await delDepartment(id)
      this.$message.success('删除成功')
      this.getDepartment()
    },
    async hCommand(command, id) {
      // console.log(id)
      this.curNodeId = id
      if (command === 'add') {
        this.showDialog = true
      } else if (command === 'edit') {
        this.showDialog = true
        this.isEait = true
        // 更新props- 异步动作
        // 直接调用了子组件的方法 同步的方法
        // 要在子组件获取数据
        // 父组件调用子组件的方法来获取数据
        this.$nextTick(() => {
          // 让子组件发送请求  给子组件帮   ref  属性
          this.$refs.dept.getDetail()
        })
      } else if (command === 'del') {
        this.$confirm('默.您确定要删除么---ps(当然)').then(() => {
          // 点击了删除  调用
          this.deldd(id)
        }).catch(() => {})
      }
    },
    // 获取组件架构
    async getDepartment() {
      const res = await getDepartment()
      // console.log(res)
      // 调用函数,数组转树状
      this.depts = transListToTreeData(res.data, 1)
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
